package com.fast.manager.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.fast.commons.spring.SpringContextHolder;
import com.fast.commons.web.model.AjaxResponse;
import com.fast.commons.web.model.DataGrid;
import com.fast.manager.system.domain.DataDictDetail;
import com.fast.manager.system.repository.DataDictDetailMapper;
import com.fast.manager.system.service.DataDictDetailService;

@Service
public class DataDictDetailServiceImpl implements DataDictDetailService {

	@Autowired
	private DataDictDetailMapper mapper ;

	@CacheEvict(value="DictDetailCache", allEntries=true)
	@Override
	public AjaxResponse add(DataDictDetail record) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		if(this.mapper.existsFileldValue(null, "dictName", record.getDictName()) > 0) 
			return AjaxResponse.failMsg("该名称已存在！") ;
		if(this.mapper.existsFileldValue(null, "code", record.getCode()) > 0) 
			return AjaxResponse.failMsg("该代码已存在！") ;
		
		record.setSortCode(this.mapper.nextSort(record.getPid(), record.getId())) ;
		this.mapper.insert(record) ;
		ajaxResponse.setObj(record);
		return ajaxResponse;
	}

	@CacheEvict(value="DictDetailCache", allEntries=true)
	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id);
		return SpringContextHolder.getBean(AjaxResponse.class).reset();
	}

	@CacheEvict(value="DictDetailCache", allEntries=true)
	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@CacheEvict(value="DictDetailCache", allEntries=true)
	@Override
	public AjaxResponse update(DataDictDetail record) {
		AjaxResponse ajaxResponse = SpringContextHolder.getBean(AjaxResponse.class).reset() ;
		
		if(this.mapper.existsFileldValue(record.getId(), "dictName", record.getDictName()) > 0) 
			return AjaxResponse.failMsg("该名称已存在！") ;
		if(this.mapper.existsFileldValue(record.getId(), "code", record.getCode()) > 0) 
			return AjaxResponse.failMsg("该代码已存在！") ;
		
		DataDictDetail DataDictDetail = this.get(record.getId()) ;
		if (null != record.getPid() && !"".equals(record.getPid())) {
			if(DataDictDetail.getId().equals(record.getPid())) {
				ajaxResponse.setStatus(false);
				ajaxResponse.setMessage("操作有误，父模块服务关联自己！");
				return ajaxResponse ;
			}
		}
		
		this.mapper.updateByPrimaryKey(record);
		return ajaxResponse;
	}

	
	@Cacheable(value="DictDetailCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"(\"+#id+\")\")")
	@Override
	public DataDictDetail get(long id) {
		return this.mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<DataDictDetail> getAllList(DataDictDetail record) {

		return null;
	}

	@Override
	public DataGrid<DataDictDetail> datagrid(DataDictDetail conditions) {

		return null;
	}
	
	@Cacheable(value="DictDetailCache" ,key="#root.targetClass +\".\"+ #root.methodName")
	@Override
	public List<DataDictDetail> findAllDataDictDetailRoot() {
		return this.mapper.findAllDataDictDetailRoot();
	}


	@Cacheable(value="DictDetailCache" ,key="#root.targetClass +\".\"+ #root.methodName")
	@Override
	public List<DataDictDetail> treegrid() {
		List<DataDictDetail> dataDictDetails = new ArrayList<DataDictDetail>() ;
		
		List<DataDictDetail> details = this.findAllDataDictDetailRoot() ;
		for (DataDictDetail DataDictDetail : details) {
			dataDictDetails.add(this.recursiveDataDictDetail(DataDictDetail)) ;
		}
		return dataDictDetails;
	}
	
	
	@Cacheable(value="DictDetailCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"(\"+#dataDictId+\")\")")
	@Override
	public DataGrid<DataDictDetail> datagrid(Integer dataDictId) {
		DataGrid<DataDictDetail> dg = new DataGrid<DataDictDetail>();
		dg.setRows(this.mapper.findDataDictDetailByDataDict(dataDictId));
		return dg;
	}
	
	
	@Cacheable(value="DictDetailCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"(\"+#dataDictId+\")\")")
	@Override
	public List<DataDictDetail> treegrid(Integer dataDictId) {
		List<DataDictDetail> dataDictDetails = new ArrayList<DataDictDetail>() ;
		
		List<DataDictDetail> details = this.mapper.findDataDictDetailByDataDict(dataDictId) ;
		for (DataDictDetail detail : details) {
			dataDictDetails.add(this.recursiveDataDictDetail(detail)) ;
		}
		return dataDictDetails;
	}
	
	/**
	 * 递归
	 * @param entity
	 * @return
	 */
	private DataDictDetail recursiveDataDictDetail(DataDictDetail entity) {
		entity.setText(entity.getDictName());
		List<DataDictDetail> childs = this.mapper.findDataDictDetailChild(entity.getId().intValue());
		if(null != childs && !childs.isEmpty()) {
			List<DataDictDetail> children = new ArrayList<DataDictDetail>() ;
			
			for (DataDictDetail r : childs) {
				if(null != r) {
					children.add(recursiveDataDictDetail(r)); 
				}
			}
			entity.setChildren(children);
		}
		return entity ;
	}

	@Cacheable(value="DictDetailCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"(\"+#dict_code+\")\")")
	@Override
	public List<DataDictDetail> getDictCombobox(String dict_code) {
		return this.mapper.getDictCombobox(dict_code);
	}

}
